package dao;

import javax.persistence.EntityExistsException;
import javax.persistence.TransactionRequiredException;

import excepciones.DaoException;
import modelo.Estado;
import modelo.Model;

public class EstadoDao extends AbstractDao {
	
	
	/**
	* Class Constructor [Empty]
	*/
	public EstadoDao()
	{
	   super();
	}
	
	/**
    * Class Constructor
    * @param model   The Usuario model to manage persistence for
    */
    public EstadoDao(Estado EstadoModel)
    {
      super(EstadoModel);      
      this.model = (Estado) model;
    }

	@Override
	public Model findById(Long id) throws DaoException {
		// TODO Auto-generated method stub
		try
	      {
	         model = (Estado) super.findById(Estado.class, id);
	      }
	      catch(Exception e)
	      {
	         e.printStackTrace();
	      }
	      
	      
	      return (Estado) model;
	}

	@Override
	public Model getModel() {
		// TODO Auto-generated method stub
		return (Estado) model;
	}
	
	public void createAccess() throws DaoException
	   {
	      startTxn();
	      try
	      {
	         entityManager.persist( model );
	         entityManager.getTransaction().commit();
	      }
	      catch(EntityExistsException e)
	      {
	         entityManager.getTransaction().rollback();
	         e.printStackTrace();
	         throw new DaoException("Create Failed: " + e.getMessage());
	      }
	      catch(IllegalArgumentException e)
	      {
	         entityManager.getTransaction().rollback();
	         e.printStackTrace();
	         throw new DaoException("Create Failed: " + e.getMessage());
	      }
	      catch(TransactionRequiredException e)
	      {
	         entityManager.getTransaction().rollback();
	         e.printStackTrace();
	         throw new DaoException("Create Failed: " + e.getMessage());
	      }
	   }

	   public Estado encontrarPorClave(String clave) throws DaoException
	   {
	      try
	      {
	         model = (Estado) super.getEntityManager().
	                 createQuery("from Estado as e where e.clave = ?1")
	                  .setParameter(1, clave)
	                  .getSingleResult();
	         
	      }
	      catch(IllegalStateException e)
	      {
	         throw new DaoException("Find by Email Failed: " + e.getMessage());
	      }
	      catch(IllegalArgumentException e)
	      {
	         throw new DaoException("Find by Email Failed: " + e.getMessage());
	      }
	      catch(Exception e)
	      {
	         throw new DaoException("Find by Email Failed: " + e.getMessage());
	      }
	      
	      
	      return (Estado) model;
	   }
	
}
